<?php


namespace app\service\model;


use app\service\Y;

class CategoryModel extends BaseModel
{

    protected $table = 'category';

    protected $append = ['big_image', 'min_image', 'big_recommend_image', 'min_recommend_image'];

    /**
     * 小缩略图
     * @param $value
     * @param $data
     * @return array|string|string[]
     */
    public function getMinImageAttr($value, $data) {
        if ($data['image']) {
            return str_replace('.jpg', '-min.jpg', $data['image']);
        }
        return '';
    }

    /**
     * 大缩略图
     * @param $value
     * @param $data
     * @return array|string|string[]
     */
    public function getBigImageAttr($value, $data) {
        if ($data['image']) {
            return str_replace('.jpg', '-big.jpg', $data['image']);
        }
        return '';
    }

    /**
     * 小缩略图
     * @param $value
     * @param $data
     * @return array|string|string[]
     */
    public function getMinRecommendImageAttr($value, $data) {
        if ($data['recommend_image']) {
            return str_replace('.jpg', '-min.jpg', $data['recommend_image']);
        }
        return '';
    }

    /**
     * 大缩略图
     * @param $value
     * @param $data
     * @return array|string|string[]
     */
    public function getBigRecommendImageAttr($value, $data) {
        if ($data['recommend_image']) {
            return str_replace('.jpg', '-big.jpg', $data['recommend_image']);
        }
        return '';
    }

    /**
     * 关联分类下的商品
     * @return \think\model\relation\HasMany
     */
    public function goods(): \think\model\relation\HasMany
    {
        return $this->hasMany(GoodsModel::class, 'category_id', 'id');
    }

    /**
     * 保存数据
     * @param $param
     * @param $id
     * @return bool|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function saveData($param, $id) {
        //
        if ($param['pid'] > 0) {
            $p_category = self::where('id', '=', $param['pid'])->find();
            if (!$p_category) {
                return Y::error('上级分类不存在');
            }
            if ($p_category->level == 3) {
                return Y::error('最多只能添加三级分类');
            }
            $param['level'] = $p_category->level + 1;
        } else {
            $param['level'] = 1;
        }

        $old_image = $old_recommend_image = '';

        if ($id > 0) {
            if (self::where('title', '=', $param['title'])->where('id', '<>', $id)->find()) {
                return Y::error('分类名已存在');
            }
            $data = self::find($id);
            if (!$data) {
                return Y::error('参数错误');
            }
            $old_image = $data->image != input('param.image', '') ? $data->image : '';
            $old_recommend_image = $data->recommend_image != input('param.recommend_image', '') ? $data->recommend_image : '';

        } else {
            if (self::where('title', '=', $param['title'])->find()) {
                return Y::error('分类名已存在');
            }
            $data = new self();
        }

        $image = input('param.image', '');
        $param['image'] = event('image_storage', [ 'image' => $image, 'type' => 'category'], 'true');
        $param['is_recommend'] = input('param.is_recommend', 0);
        if ($recommend_image = input('param.recommend_image', '')) {
            if ($param['is_recommend'] == 1 && !$recommend_image) {
                return Y::error('推荐分类必须上传推荐位背景图片');
            }
        }
        $param['recommend_image'] = event('image_storage', ['image' => $recommend_image, 'type' => 'category_recommend'], 'true');

        $data->save($param);

        unlink_old_image(compact('old_image', 'old_recommend_image'));

        return Y::success([], '保存成功');
    }
}